package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yidu.comment.Tools;
import com.yidu.dao.SnAuditMapper;
import com.yidu.dao.SnComInDetailMapper;
import com.yidu.dao.SnComInventoryMapper;
import com.yidu.dao.SnComSaDetailMapper;
import com.yidu.dao.SnComSalesMapper;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.dao.SnFiDetailMapper;
import com.yidu.dao.SnFinanceMapper;
import com.yidu.dao.SnReDetailMapper;
import com.yidu.dao.SnRetailMapper;
import com.yidu.model.SnAudit;
import com.yidu.model.SnComInDetail;
import com.yidu.model.SnComInventory;
import com.yidu.model.SnComSaDetail;
import com.yidu.model.SnComSales;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnFiDetail;
import com.yidu.model.SnFinance;
import com.yidu.model.SnReDetail;
import com.yidu.model.SnRetail;
import com.yidu.model.SnUser;
import com.yidu.service.SnComSaDetailService;
/**
 * 退货详情实现类
 * @author chensituo
 * @date 2018年12月25日 下午10:30:24
 */
@Service
public class SnComSaDetailServiceImpl implements SnComSaDetailService{
	
	/**
	 * 注入退货mapper
	 */
	@Resource
	SnComSalesMapper comSalesMapper;
	
	/**
	 * 注入退货详情mapper
	 */
	@Resource
	SnComSaDetailMapper comSaDetailMapper;
	/**
	 * 注入零售mapper
	 */
	@Resource
	SnRetailMapper retailMapper;
	/**
	 * 注入零售详情mapper
	 */
	@Resource
	SnReDetailMapper reDetailMapper;
	
	
	
	/**
	 * 注入审核mapper
	 */
	@Resource
	SnAuditMapper auditMapper;

	/**
	 * 注入商品库存mapper
	 */
	@Resource
	SnComInventoryMapper comInventoryMapper;
	/**
	 * 注入商品库存记录mapper
	 */
	@Resource
	SnComInDetailMapper comInDetailMapper;
	
	/**
	 * 注入商品mapper
	 */
	@Resource
	SnCommodityMapper commodityMapper;
	/**
	 * 注入财务mapper
	 */
	@Resource
	SnFinanceMapper financeMapper;
	
	/**
	 * 注入财务记录mapper
	 */
	@Resource
	SnFiDetailMapper fiDetailMapper;
	
	
	
	@Override
	public List<SnComSaDetail> findRetailAll(Map<String, Object> maps) {
		return comSaDetailMapper.findRetailAll(maps);
	}

	@Override
	public int findRetailCount(Map<String, Object> maps) {
		return comSaDetailMapper.findRetailCount(maps);
	}

	@Override
	public int updateAlear(String sum,String comment) {
		String [] arr=sum.split("@");
		String scId="";
		int one=0;
		int two=0;
		int three=0;
		for (String string : arr) {
			String[] str=string.split(",");
			//根据退货详情id查询
			SnComSaDetail comSaDetail=comSaDetailMapper.selectByPrimaryKey(str[0]);
			comSaDetail.setSdAlready(Integer.valueOf(str[1]));
			scId=comSaDetail.getCsId();
			//调用根据退货详情id修改的方法
			one=comSaDetailMapper.updateByPrimaryKeySelective(comSaDetail);
		}
		//调用根据退货id查询 退货订单的方法
		SnComSales comSales=comSalesMapper.selectByPrimaryKey(scId);
		//将退货意见保存
		comSales.setCsComment(comment);
		//调用退货表修改的方法
		System.err.println("---------------添加退货意见-----------------");
		comSalesMapper.updateByPrimaryKeySelective(comSales);
		//调用根据业务id查询零售的方法
		SnRetail snRetail=retailMapper.selectByPrimaryKey(comSales.getAllId());
		snRetail.setReState(3);
		//调用零售修改的方法
		two=retailMapper.updateByPrimaryKeySelective(snRetail);
		
		SnAudit snAudit=new SnAudit();
		snAudit.setAuId(Tools.getDateOrderNo());
		snAudit.setAllId(scId);
		snAudit.setAuState(0);
		snAudit.setAuComment("零售审核");
		snAudit.setOptime(Tools.getCurDateTime());
		three=auditMapper.insert(snAudit);
		if(one==1 && two==1 && three==1) {
			return 1;
		}else {
			return 0;
		}
		
	}

	@Override
	public int findQue(SnComSaDetail comSaDetail) {
		//根据零售id查询
		SnRetail snRetail=retailMapper.selectByPrimaryKey(comSaDetail.getAllId());
		snRetail.setReState(5);
		//调用修改销售表的方法
		int rows=retailMapper.updateByPrimaryKeySelective(snRetail);
		
		System.err.println("+-----------------------减少财务---------------------------");
		//根据分公司id查询财务
		SnFinance snFinance=financeMapper.selectBraId(snRetail.getBraId());
		
		
		System.err.println("+-----------------------增加库存---------------------------");
		//根据零售id查询退货单
		SnComSales comSales=comSalesMapper.findAllId(comSaDetail.getAllId());
		//调用根据零售id查询退货订单的方法
		List<SnComSaDetail> list=comSaDetailMapper.findCsId(comSales.getCsId());
		//遍历
		for (SnComSaDetail snComSaDetail : list) {
			//根据商品id查询商品
			SnCommodity commodity=commodityMapper.selectByPrimaryKey(snComSaDetail.getOper());
			System.out.println("..........."+commodity.getComName());
			
			//创建map
			Map<String, Object> map=new HashMap<>();
			map.put("comId", snComSaDetail.getOper());
			map.put("braId", snRetail.getBraId());
			
			//调用根据商品id和分公司id查询库存的方法
			SnComInventory comInventory=comInventoryMapper.findComBranId(map);
			System.out.println("------"+snComSaDetail.getSdAlready());
			comInventory.setCiNumber(new BigDecimal(comInventory.getCiNumber().intValue()+snComSaDetail.getSdAlready().intValue()));
			//调用修改库存的方法
			comInventoryMapper.updateByPrimaryKeySelective(comInventory);
			
			///创建库存详情对象
			SnComInDetail comInDetail=new SnComInDetail();
			//赋值
			comInDetail.setCidId(Tools.getDateOrderNo());
			comInDetail.setCiId(comInventory.getCiId());
			comInDetail.setIsva(1);
			comInDetail.setOptime(Tools.getCurDateTime());
			comInDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			comInDetail.setCidExplain("零售退货：商品"+commodity.getComName()+"增加库存"+snComSaDetail.getSdAlready()+commodity.getComUnit());
			//调用增加库存详情的方法
			comInDetailMapper.insert(comInDetail);
			
		}
		
		List<SnReDetail> list2=reDetailMapper.findReId(comSaDetail.getAllId());
		for (SnReDetail snReDetail : list2) {
			snFinance.setFinMoney(new BigDecimal(snFinance.getFinMoney().doubleValue()-snReDetail.getReSum().doubleValue()));
			//调用财务修改的方法
			financeMapper.updateByPrimaryKeySelective(snFinance);
			
			//创建财务记录对象
			SnFiDetail fiDetail=new SnFiDetail();
			//赋值 
			fiDetail.setFdId(Tools.getDateOrderNo());
			fiDetail.setFinId(snFinance.getFinId());
			fiDetail.setIsva(1);
			fiDetail.setOptime(Tools.getCurDateTime());
			fiDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			fiDetail.setFdMoney(snRetail.getReSum());
			fiDetail.setFdExplain("商品零售退货减少金额"+(snReDetail.getReSum())+"元");
			//调用增加财务记录的方法
			fiDetailMapper.insert(fiDetail);
		}
		
		
		
		return rows;
	}
}
